package com.example.videomedian;

import android.Manifest;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.display.DisplayManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;



import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;


public class VideoActivity extends AppCompatActivity {

    public static List<MediaBean> list=new ArrayList<>();


    GridView lv;

    public static VideoAdapter adapter;
    private Button videoreturn;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.video_activity);

        lv = findViewById(R.id.lv);
        videoreturn = findViewById(R.id.videoreturn);


        if (Build.VERSION.SDK_INT>26&&checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){
            requestPermissions(new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE
            },1);
        }else {
            xx();
        }
        videoreturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                System.exit(0);
            }
        });

        Intent intent=getIntent();
        int pos2=intent.getIntExtra("pos",0);
        String time=intent.getStringExtra("time");
        list.get(pos2).lastTime=time;



        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                String[] appTitles = new String[]{"主屏、副屏同时播放", "仅副屏播放"};

                final AlertDialog.Builder builder = new AlertDialog.Builder(VideoActivity.this);
                builder.setTitle("请选择:");
                builder.setItems(appTitles, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {


                        if (which == 0) {

                            Intent intent = new Intent(VideoActivity.this, Video2Activity.class);
                            intent.putExtra("video", position);
                            startActivity(intent);


                            VideoFuping(position);
                        } else {
                            VideoFuping(position);

                        }
                    }
                });
                builder.show();
            }
        });
    }

     private void xx(){

        getVideoFile(list,Environment.getExternalStorageDirectory());
        adapter=new VideoAdapter();
        lv.setAdapter(adapter);

     }




    public static void getVideoFile(final List<MediaBean> list, File file) {
        file.listFiles(new FileFilter() {
            @Override
            public boolean accept(File file) {
                String name = file.getName();
                if (name.endsWith(".mp4") || name.endsWith(".3gp") || name.endsWith(".wmv")){

                    MediaBean video = new MediaBean();
                    video.vName = file.getName();

                    video.path = file.getAbsolutePath();

                    list.add(video);

                } else if (file.isDirectory()) {
                    getVideoFile(list, file);
                }

                return false;
            }
        });
    }
    public class VideoAdapter extends BaseAdapter {


        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int i) {
            return 0L;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            view = LayoutInflater.from(VideoActivity.this).inflate(R.layout.video_item, null);

            ImageView img = view.findViewById(R.id.imageView);
            TextView tv = view.findViewById(R.id.videoName);
            TextView time = view.findViewById(R.id.videoTime);

            MediaBean bean = list.get(position);


            time.setText("上次看到：" + bean.lastTime);


            img.setImageBitmap(bean.getvImg());
            tv.setText(bean.vName);

            return view;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void VideoFuping(int play) {
        DisplayManager displayManager = (DisplayManager) VideoActivity.this.getSystemService(Context.DISPLAY_SERVICE);

        Display[] displays = displayManager.getDisplays();
        if (displays.length > 1) {

            Intent intent = new Intent(VideoActivity.this, VideoFuPing.class);
            intent.putExtra("play", play);
            intent.setClassName(getPackageName(), getPackageName() + ".VideoFuPing");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);

            ActivityOptions options = null;

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                options = ActivityOptions.makeBasic();
            }

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                options.setLaunchDisplayId(displays[1].getDisplayId());


            }

            startActivity(intent, options.toBundle());

        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode==1&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
            xx();
        }else {
            setTitle("已拒绝权限，暂无数据");
        }
    }
}





